package com.ys.otms.utils.ali;

import com.aliyun.mns.client.CloudQueue;
import com.aliyun.mns.model.Message;
import com.aliyuncs.exceptions.ClientException;
import java.text.ParseException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DefaultAlicomMessagePuller {
    private String mnsAccountEndpoint = "https://1943695596114318.mns.cn-hangzhou.aliyuncs.com/";
    private String endpointNameForPop = "cn-hangzhou";
    private String regionIdForPop = "cn-hangzhou";
    private String domainForPop = "dybaseapi.aliyuncs.com";
    private TokenGetterForAlicom tokenGetter;
    private MessageListener messageListener;
    private Log logger = LogFactory.getLog(DefaultAlicomMessagePuller.class);
    private boolean isRunning = false;

    public DefaultAlicomMessagePuller() {
    }

    public void startReceiveMsg(String accessKey, String accessSecret, String messageType, String queueName, MessageListener messageListener) throws ClientException, ParseException {
        this.tokenGetter = new TokenGetterForAlicom(accessKey, accessSecret, this.endpointNameForPop, this.regionIdForPop, this.domainForPop);
        this.messageListener = messageListener;
        this.isRunning = true;
        DefaultAlicomMessagePuller.PullMessageTask task = new DefaultAlicomMessagePuller.PullMessageTask();
        task.messageType = messageType;
        task.queueName = queueName;
        Thread thread = new Thread(task, "PullMessageTask-thread");
        thread.start();
    }

    public void stop() {
        this.isRunning = false;
    }

    private class PullMessageTask implements Runnable {
        private String messageType;
        private String queueName;

        private PullMessageTask() {
        }

        public void run() {
            while(DefaultAlicomMessagePuller.this.isRunning) {
                try {
                    TokenForAlicom tokenObject = DefaultAlicomMessagePuller.this.tokenGetter.getTokenByMessageType(this.messageType, this.queueName, DefaultAlicomMessagePuller.this.mnsAccountEndpoint);
                    CloudQueue queue = tokenObject.getQueue();
                    Message popMsg = queue.popMessage();
                    if (popMsg != null) {
                        boolean dealResult = DefaultAlicomMessagePuller.this.messageListener.dealMessage(popMsg);
                        if (dealResult) {
                            queue.deleteMessage(popMsg.getReceiptHandle());
                        }
                    } else {
                        Thread.sleep(3000L);
                    }
                } catch (Throwable var6) {
                    DefaultAlicomMessagePuller.this.logger.error("PullMessageTask_execute_error,messageType:" + this.messageType + ",queueName:" + this.queueName, var6);

                    try {
                        Thread.sleep(3000L);
                    } catch (InterruptedException var5) {
                        DefaultAlicomMessagePuller.this.logger.error("PullMessageTask_execute_error,messageType:" + this.messageType + ",queueName:" + this.queueName, var6);
                    }
                }
            }

        }
    }
}
